<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Cat;
use App\Stadium;
use DB;

class IndexController extends Controller
{
	public $category;
	public function __construct(){
        $this->category = Cat::all()->toArray();
    }

   


    public function getTree($pid = 0, $lev = 0)
    {
        $tree = array();
        foreach($this->category as $v){
            if($v['pid'] == $pid){
                $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree, $this->getTree($v['cat_id'], $lev+1));
            }
        }
        return $tree;
   	}


	public function help()
	{
		return view('help');
	}


	public function getCats()
	{
		return 	DB::table('Cats')->get();
	}
		

	public function getSon($cat_id)
	{
		$data = $this->getCats();
		$tree= array();
		foreach($data as $v)
		{
			if($v->pid == $cat_id)
			{
				$tree[] = $v->cat_id;

				$this->getSon($v->cat_id);
			}

		}
			
		return $tree;
	}

		
	public function getCount()
	{
		$data = $this->getCats();
		$sum = array();
		foreach($data as $v)
		{
			if($v->pid == 0)
			{
				
				$sum[$v->cat_id] = $v;

				$sons = $this->getSon($v->cat_id);
				// echo '<pre>';
				// print_r($v->cat_id);
				// $sonss = implode(',',$sons);
				// $count = D('goods')->where(array('cat_id'=>array('in',$sonss)))->count();
				$count = DB::table('stadiums')->whereIn('cat_id',$sons)->count();
				
				$sum[$v->cat_id]->count = $count;
			}
		}

		return $sum;
	}


	public function index()
   {
   		$cats = $this->getCount();
        return view('index',['cats'=>$cats]);
   }
}
